package org.ymjava.medical.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ymjava.medical.core.util.ResponseUtil;
import org.ymjava.medical.db.AllPackage.biz.PrepareForUserBiz;
import org.ymjava.medical.db.AllPackage.entity.PrepareForUser;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wx_answer/prepare_record")
public class PrepareRecordController {

    private final Log logger = LogFactory.getLog(PrepareRecordController.class);
    @Autowired
    PrepareForUserBiz prepareForUserBiz;


    /**
     *
     * @param userId  用户id
     * @param questionId 问题id
     * @param typeId  类型
     * @param position  位置记录
     * @param bankId  题库id
     * @param classId   备考分类
     * @param wrong   是否是错题
     * @return
     */
    @GetMapping("/add_record")
    public Object add_record(Integer userId, Integer questionId,Integer typeId, Integer position,Integer bankId, Integer classId,Integer wrong){

        //查询是否有记录

        PrepareForUser prepareForUser = new PrepareForUser();

        prepareForUser.setUserId(userId);
        prepareForUser.setType(typeId);
        prepareForUser.setPrepareId(classId);
        prepareForUser.setWrong(null);
        prepareForUser.setDeleted(0);

        List<PrepareForUser> list_prepareForUser = prepareForUserBiz.queryPosition(userId,typeId,classId);

        if(list_prepareForUser.size() == 0){
            //未被添加过
            PrepareForUser new_prepareForUser = new PrepareForUser();
            new_prepareForUser.setDeleted(0);
            new_prepareForUser.setPrepareId(classId);
            new_prepareForUser.setType(typeId);
            new_prepareForUser.setUserId(userId);
            new_prepareForUser.setAddTime(new Date());
            new_prepareForUser.setBankId(bankId);
            new_prepareForUser.setPostition(position);
            new_prepareForUser.setQuestionId(questionId);
            prepareForUserBiz.insert(new_prepareForUser);
        }else{
            PrepareForUser prepareForUser1 = list_prepareForUser.get(0);
            prepareForUser1.setQuestionId(questionId);
            prepareForUser1.setBankId(bankId);
            prepareForUser1.setPostition(position);
            prepareForUser1.setAddTime(new Date());
            prepareForUserBiz.updateById(prepareForUser1);
            //更新
        }

        List<PrepareForUser> wrong_prepares = prepareForUserBiz.queryWrong(questionId,bankId);

        if(wrong_prepares.size() == 0){
            //添加对错
            PrepareForUser wrong_prepare = new PrepareForUser();
            wrong_prepare.setAddTime(new Date());
            wrong_prepare.setDeleted(0);
            wrong_prepare.setPrepareId(classId);
            wrong_prepare.setType(typeId);
            wrong_prepare.setUserId(userId);
            wrong_prepare.setAddTime(new Date());
            wrong_prepare.setBankId(bankId);
            wrong_prepare.setQuestionId(questionId);
            wrong_prepare.setWrong(wrong);
            prepareForUserBiz.insert(wrong_prepare);
        }else{
            //更新
            PrepareForUser wrong_prepare1 = wrong_prepares.get(0);
            wrong_prepare1.setWrong(wrong);
            prepareForUserBiz.updateById(wrong_prepare1);
        }
        return ResponseUtil.ok();
    }


}
